//有效的括号

import java.util.Stack;
public class validBracket {
    public Boolean bracket(String s){
        Stack<Character> stack = new Stack<>();
        char[] cs = s.toCharArray();//转为字符数组
        if (cs.length%2!=0){//不是偶数，那肯定是不可能消除完整的
            return false;
        }
        for (char c:cs){
            if(c=='('){
                stack.push(')');
            }else if(c=='['){
                stack.push(']');
            }else if(c=='{'){
                stack.push('}');
            }else if(stack.isEmpty() || stack.peek()!=c){//如果还有右括号，但是栈为空了，所以失败，如果不相等也退出
                return false;
            }else if(stack.peek()==c){
                stack.pop();
            }
        }

        if(stack.size()==0){
            return true;
        }else {
            return false;
        }
    }
    public static void main(String[] args) {
        var main = new validBracket();
        System.out.println(main.bracket("()())"));
    }
}
